from _md5 import md5
from dataclasses import dataclass
from datetime import datetime
from exts import db

@dataclass
class Article(db.Model):
    __tablename__ = "article"
    id:int = db.Column(db.Integer,primary_key=True,autoincrement=True)
    title:str = db.Column(db.String(255),nullable=False)
    content:str = db.Column(db.Text,nullable=False)
    author_id = db.Column(db.Integer,db.ForeignKey("user.id"))
    code = db.Column(db.String(255),comment="附件关联id")
    attachment = db.relationship('Attachment', secondary="attachment_relation", backref=db.backref('article'))
    #author = db.relationship("User",back_populates="articles")
    #author = db.relationship("User",backref="articles")
    #user = relationship("User", back_populates="articles")
    create_time:datetime = db.Column(db.DateTime, default=datetime.now)
    update_time:datetime = db.Column(db.DateTime, default=datetime.now,onupdate=datetime.now)
    def to_dict(self):
        user_dict = {
            'id':self.id,
            'title':self.title,
            'content':self.content,
            'author_id':self.author_id,
            'user':self.user,
            'attachment':self.attachment,
            'create_time':self.create_time,
            'update_time':self.update_time,
        }
        return user_dict